//
// File:	ResourceManager.hpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Created:	14/04/2010 at 13:53:52
// Purpose:	
//
#ifndef RESOURCEMANAGER_HPP
#define RESOURCEMANAGER_HPP

#include <map>
#include <string>
#include "Singleton.hpp"

//------------------------------------------------------------------
// Class
//------------------------------------------------------------------
template <typename T>
class ResourceManager : public Singleton<ResourceManager<T> >
{
protected:
									friend class Singleton<ResourceManager<T> >;
									ResourceManager();

public:
	T								*Find(const std::string &fileName);
	void							ClearAll();

private:
	T								*LoadFromFile(const std::string &fileName);

private:
	std::map<std::string, T *>		m_resources;
};

//------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------
template <typename T>
ResourceManager<T>::ResourceManager()
	:	m_resources()
{
}

//------------------------------------------------------------------
// Find
//------------------------------------------------------------------
template <typename T>
T *ResourceManager<T>::Find(const std::string &fileName)
{
	if (m_resources.find(fileName) != m_resources.end())
		return m_resources[fileName];
	T *pResource = LoadFromFile(fileName);
	if (pResource != NULL)
		m_resources[fileName] = pResource;
	return pResource;
}

//------------------------------------------------------------------
// Load From File
//------------------------------------------------------------------
template <typename T>
T *ResourceManager<T>::LoadFromFile(const std::string &fileName)
{
	return NULL;
}

//------------------------------------------------------------------
// Clear All
//------------------------------------------------------------------
template <typename T>
void ResourceManager<T>::ClearAll()
{
	std::map<std::string, T *>::iterator it = m_resources.begin();
	std::map<std::string, T *>::iterator ite = m_resources.end();
	for (; it != ite; ++it)
	{
		delete it->second;
		it->second = NULL;
	}
	m_resources.clear();
}

#endif //!RESOURCEMANAGER_HPP
